package KVService;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransportException;
import org.ini4j.Ini;


import Node.TMultiplexedProtocol;
import Node.TransportLayer;

public class KVClient {

	private TSocket transport;
	public KVService.Client KVProto;
	
	public KVClient(String server, int serverPort) throws TTransportException{
	
		/*
		 * Setup transport layer
		 */
		transport = new TSocket(server, serverPort);
		
		/*
		 * Initialize protocol: JSON, compact, binary, etc.
		 */
		TBinaryProtocol protocol = new TBinaryProtocol(transport);

		/*
		 * Register service (service name must match service name registered on server)
		 */
		TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol, "KVService");
		
		KVProto = new KVService.Client(mp);
	}
	
	/**
	 * Ask configured server for ID of node storing key
	 * @param key
	 * @return
	 * @throws TException 
	 */
	public int lookup(int key) throws TException   {
		// TODO Auto-generated method stub
		transport.open();
		int nodeID = KVProto.lookup(key);
		transport.close();
		
		return nodeID;
	}
}
